﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Sockets;

namespace BeiJingQuestion1
{
    public class demo1
    {
        public static void Main(string[] args)
        {
            // 创建并配置 UDP 服务实例
            var udpService = new UdpSession();
            udpService.Received = (c, e) =>
            {
                // 获取字节数组并转换为十六进制字符串打印
                Console.WriteLine("接收到数据 (HEX):");
                byte[] data = e.ByteBlock.ToArray();
                for (int i = 0; i < data.Length; i++)
                {
                    Console.Write($"{data[i]:X2} ");
                    // 每16个字节换行，提高可读性
                    if ((i + 1) % 16 == 0)
                        Console.WriteLine();
                }
                Console.WriteLine(); // 最后添加一个换行
                return EasyTask.CompletedTask;
            };
            udpService.Setup(new TouchSocketConfig()
                .SetBindIPHost(new IPHost(50789)));
            udpService.Start();
            Console.WriteLine("等待接收");
            Console.WriteLine("UDP监听已启动，监听端口：50789");
            Console.WriteLine("按任意键退出程序...");
            Console.ReadKey();

            // 退出前停止服务
            udpService.Stop();
        }
    }
}
